﻿using System.Collections;
using UnityEngine;


public class NormalBrick : Brick
{
    private void Awake()
    {
        InitPhysicsComponent();
    }
    private void OnDrawGizmos()
    {
        DrawSensorGroundLine();
    }
    private void Update()
    {
        if (!OnGrounded())
        {
            Debug.Log("下坠");
            _rb.bodyType = RigidbodyType2D.Dynamic;
            _rb.velocity = new Vector2(0,_rb.velocity.y);
        }
        else 
        {
            _rb.bodyType=RigidbodyType2D.Kinematic;
            _rb.velocity = Vector2.zero;
        }
    }

    [Header("下落")]
    [SerializeField] private Transform _groundSensor;
    [SerializeField] private LayerMask _groundLayerMask;
    [SerializeField] private float distance;
    private void DrawSensorGroundLine()
    {
        Gizmos.DrawLine(_groundSensor.position, new Vector2(_groundSensor.position.x , _groundSensor.position.y - distance));
    }
    public bool OnGrounded() 
    { 
        RaycastHit2D hit=Physics2D.Linecast(_groundSensor.position, new Vector2(_groundSensor.position.x, _groundSensor.position.y - distance), _groundLayerMask);
        
        if (hit.collider != null) 
        {
            if (hit.collider.gameObject != this) 
            {
                return true;
            }
            else 
            {
                return false;
            }
        }
        else 
        {
            return false;
        }
    }

    private Rigidbody2D _rb;
    [SerializeField] private float _fallSpeed;
    private void InitPhysicsComponent()
    {
        _rb = GetComponent<Rigidbody2D>();
    }

    private void FallBrick()
    {
        _rb.velocity = new Vector2(0, _fallSpeed);
    }
}
